﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using QLPMT_DTO;

namespace QLPMT_DAO
{
   public class DonViDAO
    {
       DataProvider provider;
       public DonViDAO()
       {
           provider = new DataProvider();
       }
       // Lay ma don vi cuoi
       //lay tat ca danh sach don vi
       public List<DonVi> GetAll()
       {
           provider.Connect();
           string sql = " select *  from DONVI";
           SqlDataReader reader = provider.ExecuteReader(sql);
           List<DonVi>list=new List<DonVi>();
           while (reader.Read())
           {
               string ma = reader.GetString(0);
               string ten = reader.GetString(1);
               DonVi dv = new DonVi(ma, ten);
               list.Add(dv);
           }
           provider.Disconnect();
           return list;
       }
       // Them Don VỊ mỚi 
       public void ThemDonViMoi(DonVi dto)
       {
           provider.Connect();
           string sql = "insert into DonVi(MaDonVi,DonVi) values('"+dto.Madonvi+"','"+dto.Tendonvi+"')";
           provider.ExecuteQuery(sql);
           provider.Disconnect();
       }
       public void SuaDonVi(DonVi dto)
       {
           provider.Connect();
           string sql = "update DonVi set DonVi='" + dto.Tendonvi + "' where MaDonVi='" + dto.Madonvi + "'";
           provider.ExecuteQuery(sql);
           provider.Disconnect();
       }
    }
}
